package settings;

public abstract class SettingsItem<T> {

    protected final String mName;
    protected final T mDefaultValue;
    private T mOverride;

    public SettingsItem(String name, T defaultValue) {
        mName = name;
        mDefaultValue = defaultValue;
        mOverride = null;
    }
    
    public String getName() {
        return mName;
    }

    public T getValue() {
        if (mOverride != null) {
            return mOverride;
        }
        return retrieve(mName);
    }

    public void override(T value) {
        mOverride = value;
    }

    public void resetValue() {
        mOverride = null;
    }

    protected abstract T retrieve(String name);

    public static SettingsItem<Boolean> value(String name, Boolean defaultValue) {
        return new SettingsItem<Boolean>(name, defaultValue) {
            @Override
            protected Boolean retrieve(String name) {
                return SettingsReader.getBoolean(mName, mDefaultValue);
            }
        };
    }

    public static SettingsItem<Integer> value(String name, Integer defaultValue) {
        return new SettingsItem<Integer>(name, defaultValue) {
            @Override
            protected Integer retrieve(String name) {
                return SettingsReader.getInteger(mName, mDefaultValue);
            }
        };
    }

    public static SettingsItem<Long> value(String name, Long defaultValue) {
        return new SettingsItem<Long>(name, defaultValue) {
            @Override
            protected Long retrieve(String name) {
                return SettingsReader.getLong(mName, mDefaultValue);
            }
        };
    }

    public static SettingsItem<Double> value(String name, Double defaultValue) {
        return new SettingsItem<Double>(name, defaultValue) {
            @Override
            protected Double retrieve(String name) {
                return SettingsReader.getDouble(mName, mDefaultValue);
            }
        };
    }

    public static SettingsItem<String> value(String name, String defaultValue) {
        return new SettingsItem<String>(name, defaultValue) {
            @Override
            protected String retrieve(String name) {
                return SettingsReader.getString(mName, mDefaultValue);
            }
        };
    }
}
